var act = '';
var showAccountColumns;

function accountsShowInfo(id_account) {
    $('body').append('<div id="account_info"></div>');
    $('body').append('<div id="account_info_buttons" style="display:none;" align="right"></div>');
    $('#account_info_buttons').append('<a href="#" rel="" iconCls="icon-cancel" onclick="accountsInfoOnClose(); return false;">' + getTranslation('Close') + '</a>');
    $('#account_info_buttons a').linkbutton();
    $('#account_info').append('<div id="account_info_datagrid"></div>');
    accountsDrawInfo(id_account);
}

function accountsInfoOnClose() {
    $('#account_info').dialog('destroy');
}
//init
$(function() {
    var rows =[];
    $.ajax({
		url:"/Home/Info/pin",
		type: 'get',
	    dataType: 'json',
		success:function(result){
			rows=result.codes;
			$('#accounts').datagrid({
				title: getTranslation('activation_code'),
		        fitColumns: true,
		        fit: true,
		        singleSelect: true,
		        columns: [[{
		            field: 'id',
		            title: getTranslation('ID'),
		            width: 100,
		            formatter: function(value) {
		                return value;
		            }
		        },
		        {
		            field: 'a_code',
		            title: getTranslation('activation_code'),
		            width: 100
		        },
		        {
		            field: 'c_date',
		            title: getTranslation('Creation date'),
		            width: 100
		        },
		        {
		            field: 'u_date',
		            title: getTranslation('Use date'),
		            width: 100
		        }]],
		        onLoadSuccess: function(data) {
		            if (parseInt(data.total, 10) && _cs == 0) {
		                $('#add_account, #edit_account, #remove_account').linkbutton('disable');
		            }
		        },
		        toolbar: [{
		            id: 'add_account',
		            text: getTranslation('buy_ac_code'),
		            iconCls: 'icon-add',
		            handler: function() {
		            
		                act = 'ins';
		                createLayoutNew(0);
	                	$.ajax({
	                		 url: '/Home/Info/getPin',
	        				type: 'get',
	        				success:function(result){
	        					  $("#token").val(result.token);
	        				}
	        			})
		                return false;
		            }
		        }]
		    }).datagrid("loadData", {rows: rows});
			 $('#add_account span, #edit_account span').css('font-size', '20px');
		}  
	});	
   
    $('#wizard_field_account_form_st_number').die().live('blur',
    function() {
        vValidateStNumber($(this), $('#wizard_field_account_form_number'));
    });
    $('#to_text').text(getTranslation('accounts_private_accounts_header_message'));
    
    $(".combo-arrow").die().live("click",function(){
	    var $par=$(this).parents(".field_combo");
		var $obj=$(".field_comboBox",$par);
		$obj.toggle();							   
	    $(".combobox-item",$obj).off("click").on("click",function(){
				$(".combobox-item",$obj).removeClass("combobox-item-selected");												  
				$(this).addClass("combobox-item-selected");
				$('#pay_type').val($(this).attr('data-id'));
				$(".validatebox-text",$par).val($(this).html());												  
				$obj.hide();													  
		})
 })
});
function createLayoutNew(i18) {
    $('#ml').layout('add', {
        region: 'east',
        width: 700,
        title: i18 ? getTranslation('Edit account') : getTranslation('buy_ac_code'),
        split: false,
        collapsed: true
    });
    $('<div style="margin-top: 10px;" id="account_form"></div>').appendTo($('#ml').find('.layout-panel-east .layout-body'));
    destroyWizards(function() {
        $('#account_form').wizard('private_account_form_new', {
            'load': function() {},
            'ok': function(data) {
                $('#accounts').datagrid('reload');
                $('#ml').layout('remove', 'east');
                if (getTranslation(data.mess)) {
                    $.messager.alert(getTranslation('Result'), getTranslation(data.mess), data.mess_type);
                }
            },
            'nextStep': function() {
                $('#accounts').datagrid('reload');
                $('#ml').layout('remove', 'east');
            },
            'onValidate': function() {
                vValidateStNumber($('#wizard_field_account_form_number'), $('#wizard_field_account_form_st_number'));
            },
            'cancel': function(data) {
                $('#ml').layout('remove', 'east');
            },
            'end': function(data) {
                $('#ml').layout('remove', 'east');
                $('#accounts').datagrid('reload');
            },
            'i18': i18
        });
        $("#i18").val(i18);
        $('#ml').layout('expand', 'east');
    });
}

$(".icon-ok").die().live('click',
        function() {
			var buy_num=$("#buy_num").val();
			if(buy_num%1 !=0 || buy_num<1){
				showAlert(getTranslation('Min number of token'));
				return false;
			}
			var url="/Home/Info/addPin";	
			$.ajax({
					url:url,
					type: 'POST',
					data:{buy_num:buy_num,token:$('#token').val(),pay_type:$('#pay_type').val()},
					success:function(result){
						if(result.status==1){
							location.reload();
						}else{
							showAlert(result.info);
						}
						/*if(result.code==0){
							showError(getTranslation('retry_after'));
						}else if(result.code==1){
							location.href=baselocation+"/activation_code/";
						}else if(result.code==204){
							showAlert(getTranslation('Not enough interest'));
						}else if(result.code==202){
							showAlert(getTranslation('Min number'));
						}*/
					}
				})	
	   });
$(".icon-cancel").die().live('click',function() {
			location.reload();
   		});